package bean;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.rich.RichPopup;

import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;

import oracle.adf.view.rich.event.DialogEvent.Outcome;

import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.NavigatableRowIterator;
import oracle.jbo.Row;
import oracle.jbo.uicli.binding.JUCtrlHierBinding;

import org.apache.myfaces.trinidad.model.CollectionModel;

import util.ADFUtil;

public class TaskDetailsBean {


    private RichPopup taskPopUp;
    private RichTable tasksTable;
    private RichPopup taskCommentsPopup;
    private RichPopup deleteTaskPopup;

    public TaskDetailsBean() {
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void setTaskPopUp(RichPopup taskPopUp) {
        this.taskPopUp = taskPopUp;
    }

    public RichPopup getTaskPopUp() {
        return taskPopUp;
    }

    public void setTasksTable(RichTable tasksTable) {
        this.tasksTable = tasksTable;
    }

    public RichTable getTasksTable() {
        return tasksTable;
    }


    public void addTask(ActionEvent actionEvent) {
        // Add event code here...
        ADFUtil.setEL("#{viewScope.operation}", "create");
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        taskPopUp.show(ph);
    }


    public void editTask(ActionEvent actionEvent) {

        ADFUtil.setEL("#{viewScope.operation}", "update");

        ///////////////comments for the tasks/////////////
        oracle.jbo.domain.Number sourceId =
            (oracle.jbo.domain.Number)ADFContext.getCurrent().getPageFlowScope().get("sourceId");
        BindingContainer bindings = getBindings();
        OperationBinding operationBindings = bindings.getOperationBinding("showComments");
        operationBindings.getParamsMap().put("sourceType", "Tasks");
        operationBindings.getParamsMap().put("sourceId", sourceId);
        operationBindings.execute();


        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        taskPopUp.show(ph);
    }


    public void deleteTask(ActionEvent actionEvent) {
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        deleteTaskPopup.show(ph);
    }


    public void taskDialog(DialogEvent dialogEvent) {
        Outcome out = dialogEvent.getOutcome();
        if (out.equals(Outcome.ok)) {
            ADFUtil.setEL("#{viewScope.operation}", "");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");
            BindingContainer bindings = getBindings();
            OperationBinding operationBindings = bindings.getOperationBinding("addComments");
            if (operationBindings != null) {
                operationBindings.getParamsMap().put("sourceType", "Tasks");
                operationBindings.execute();
            }
            AdfFacesContext.getCurrentInstance().addPartialTarget(tasksTable);
        }
    }


   

    public void deleteDilog(DialogEvent dialogEvent) {
        Outcome out = dialogEvent.getOutcome();
        if (out.equals(Outcome.ok)) {
            ADFUtil.invokeEL("#{bindings.Delete.execute}");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");

        }
    }

    public void taskClose(PopupCanceledEvent popupCanceledEvent) {
        String checkForRollBack = (String)ADFContext.getCurrent().getViewScope().get("operation");
        if (checkForRollBack != null && checkForRollBack.equals("create")) {
            DCBindingContainer bindings = (DCBindingContainer)getBindings();
            DCIteratorBinding dcIter = bindings.findIteratorBinding("PrjTasksIterator");
            Row row = dcIter.getCurrentRow();
            OperationBinding operationBindings = bindings.getOperationBinding("rollBackForTasks");
            operationBindings.getParamsMap().put("r", row);
            operationBindings.execute();

        }
        ADFUtil.setEL("#{viewScope.operation}", "");
        AdfFacesContext.getCurrentInstance().addPartialTarget(tasksTable);
        // ADFUtil.invokeEL("#{bindings.Rollback.execute}");


    }

    public void sourceTypeSearchCriteria(ActionEvent actionEvent) {

        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        taskCommentsPopup.show(ph);
    }

    public void setTaskCommentsPopup(RichPopup taskCommentsPopup) {
        this.taskCommentsPopup = taskCommentsPopup;
    }

    public RichPopup getTaskCommentsPopup() {
        return taskCommentsPopup;
    }

    public void setDeleteTaskPopup(RichPopup deleteTaskPopup) {
        this.deleteTaskPopup = deleteTaskPopup;
    }

    public RichPopup getDeleteTaskPopup() {
        return deleteTaskPopup;
    }
}
